package gui;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import beans.Product;
import business.ProductBusiness;

import com.example.preconaprateleira.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;



public class BarcodeScanActivity extends Activity implements OnClickListener {
	private Button scanBtn;
	private TextView formatTxt, contentTxt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_barcode_scan);
		
		scanBtn = (Button)findViewById(R.id.scan_button);
		formatTxt = (TextView)findViewById(R.id.scan_format);
		contentTxt = (TextView)findViewById(R.id.scan_content);
		
		scanBtn.setOnClickListener(this);
	//	IntentIntegrator scanIntegrator = new IntentIntegrator(this);
	//	scanIntegrator.initiateScan();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.scan_button){
			//scan
			
			
			ProductBusiness productBusiness = new ProductBusiness();
			
			Product product = new Product();
			product.setEANProduct("1234567891234");
			product.setName("Teste");
			
			Product product1 = new Product();
			product1.setEANProduct("1134567891234");
			product1.setName("Teste1");
			
			Product product2 = new Product();
			product2.setEANProduct("1334567891234");
			product2.setName("Teste2");
			
			Product product3 = new Product();
			product3.setEANProduct("1434567891234");
			product3.setName("Teste3");
			
			
			
			productBusiness.insertProduct(product, this);
			productBusiness.insertProduct(product1, this);
			productBusiness.insertProduct(product2, this);
			productBusiness.insertProduct(product3, this);
			
			Log.d("Teste", String.valueOf(productBusiness.returnListProducts(this).size()));
			
			IntentIntegrator scanIntegrator = new IntentIntegrator(this);
			scanIntegrator.initiateScan();
			
			
			}
		
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		//retrieve scan result
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (scanningResult != null) {
			Log.d("tESTEEE", "ESDDDDDDDDDDDDDDDDDDDDDDD");
			//we have a result
			String scanContent = scanningResult.getContents();
			String scanFormat = scanningResult.getFormatName();
			
			contentTxt.setText("CONTENT: " + scanContent);
			
			
//			if (scanContent.equalsIgnoreCase("7791293022550")){
//				contentTxt.setText("CONTENT: " + "Rexona Men 150ml");
//			}
//			else if (scanContent.equalsIgnoreCase("7891024130728")){
//				contentTxt.setText("CONTENT: " + "Colgate Plax 60ml");
//			}
//			else{
//				contentTxt.setText("CONTENT: " + scanContent);
//			}
//			formatTxt.setText("FORMAT: " + scanFormat);
//			
			}
		else{
		    Toast toast = Toast.makeText(getApplicationContext(),
		        "No scan data received!", Toast.LENGTH_SHORT);
		    toast.show();
		}
		}
}
